import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { addRouter } from "./addRoutesFun";
import { useCounterStore } from "@/stores/counter.js";
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  // history: createWebHistory("/"),
  routes: [
    {
      path: "/",
      redirect: "/layout",
    },
    {
      path: "/layout",
      name: "layout",
      component: () => import("../views/layout/Index.vue"),
      redirect: "/system",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/test",
      name: "test",
      component: () => import("../views/test.vue"),
    },
    {
      path: "/:error*",
      name: "404",
      component: () => import("@/views/Not404.vue"),
    },
  ],
});

let list = localStorage.getItem("manpu_menuList");
let token = localStorage.getItem("manpu_token");

if (list) {
  addRouter(router, JSON.parse(list));
} else {
  router.push("/login");
}
router.beforeEach((to, from, next) => {
  let list_data = localStorage.getItem("manpu_menuList");
  let token_data = localStorage.getItem("manpu_token");
  if (to.path == "/login") {
    next();
  } else {
    if (list_data && token_data) {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
